<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<link rel="shortcut icon" type="image/x-icon" href="${contextPath}/favicon.ico"/>
<link rel="stylesheet" type="text/css" href="${contextPath}/resource/widget/css/widget.css"/>
<link rel="stylesheet" type="text/css" href="${contextPath}/resource/webcat/css/form.css"/>
<script type="text/javascript" src="${contextPath}/resource/webcat/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
<!--
var DataURI = {};

DataURI.open = function() {
    var inputId = "fileInput";
    var input = this.getFileInput(inputId, inputId, "*", false);
    var onchange = function(event) {
        var e = (event || window.event);
        var input = e.target;
        var files = input.files;

        if(files == null || files.length < 1) {
            input.parentNode.parentNode.removeNode(input.parentNode);
            return;
        }
        DataURI.select(input.files[0]);
    };
    this.addListener(input, "change", onchange);
    input.click();
};

DataURI.select = function(blob, callback) {
    var fileReader = new FileReader();

    this.addListener(fileReader, "load", function(e) {
        var dataURL = e.target.result;

        jQuery("#preview").html("<img src=\"" + dataURL + "\"/>");
        jQuery("#source").val(dataURL);
        jQuery("#source").select();
    });
    fileReader.readAsDataURL(blob);
};

DataURI.getFileInput = function(id, name, accept, multiple) {
    var input = document.getElementById(id);

    if(input == null) {
        var params = {
            "id": id,
            "name": name,
            "type": "file",
            "accept": (accept || "*"),
            "multiple": multiple
        };

        input = this.createElement("input", params);

        var div = this.createElement("div");
        div.style.display = "none";
        div.appendChild(input);
        document.body.appendChild(div);
    }
    return input;
};

DataURI.createElement = function(name, params) {
    var e = null;

    if(window.ActiveXObject) {
        e = document.createElement("<" + name + ">");
    }
    else {
        e = document.createElement(name);
    }

    if(params != null) {
        for(var i in params) {
            if(params[i] != null) {
                e.setAttribute(i, params[i]);
            }
        }
    }
    return e;
};

DataURI.addListener = function(target, type, handler) {
    if(target == null) {
        return;
    }

    if(target.attachEvent) {
        target.attachEvent("on" + type, handler);
    }
    else if(target.addEventListener) {
        target.addEventListener(type, handler, false);
    }
    else {
        target["on" + type] = handler;
    }
};

jQuery(function() {
    jQuery("#select-file-btn").click(function() {
        DataURI.open();
    });

    jQuery("#show-image-btn").click(function() {
        jQuery("#preview").html("<img src=\"" + jQuery("#source").val() + "\"/>");
    });
});
//-->
</script>
</head>
<body>
<div style="margin: 0px auto 0px auto; width: 1000px;">
    <div class="h10"></div>
    <div class="form">
        <div class="button">
            <button id="select-file-btn" class="button">选择文件</button>
            <button id="show-image-btn" class="button">显示图片</button>
        </div>
        <div><textarea id="source" style="width: 100%; height: 100px; box-sizing: border-box;" spellcheck="false"></textarea></div>
        <h2 style="margin-top: 10px; background-color: #f8f8f8; line-height: 30px; color: #666666;">预览</h2>
        <div id="preview" style="display: inline-block; margin-top: 20px; padding: 30px; border: 1px solid #efefef;"></div>
    </div>
</div>
<div class="h20"></div>
</body>
</html>
